
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Marwen
 */
public class AjouterEntreprise extends javax.swing.JFrame {

    /**
     * Creates new form AjouterEntreprise
     */
    public AjouterEntreprise() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        effacer = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jPanel2 = new javax.swing.JPanel();
        valAction = new javax.swing.JTextField();
        phone = new javax.swing.JTextField();
        adresse = new javax.swing.JTextField();
        entreprise = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setBackground(new java.awt.Color(255, 255, 255));
        jButton1.setText("Valider");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        effacer.setBackground(new java.awt.Color(255, 255, 255));
        effacer.setText("Effacer");
        effacer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                effacerActionPerformed(evt);
            }
        });

        jSeparator1.setBackground(new java.awt.Color(250, 50, 0));
        jSeparator1.setForeground(new java.awt.Color(0, 0, 0));

        jSeparator2.setBackground(new java.awt.Color(250, 50, 0));
        jSeparator2.setForeground(new java.awt.Color(0, 0, 0));

        valAction.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                valActionKeyTyped(evt);
            }
        });

        phone.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                phoneKeyTyped(evt);
            }
        });

        adresse.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                adresseKeyTyped(evt);
            }
        });

        entreprise.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                entrepriseKeyTyped(evt);
            }
        });

        jLabel5.setText("Valeur de l'action ");

        jLabel4.setText("Télèphone");

        jLabel2.setText("Nom Entreprise");

        jLabel3.setText("Adresse");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 119, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(valAction, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(entreprise, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(adresse, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(phone, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(entreprise, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(adresse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(phone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(valAction, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 605, Short.MAX_VALUE)
                    .addComponent(jSeparator2))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(147, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(61, 61, 61)
                        .addComponent(effacer, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(188, 188, 188))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(113, 113, 113))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(effacer)
                    .addComponent(jButton1))
                .addGap(29, 29, 29))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void entrepriseKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_entrepriseKeyTyped
    if (entreprise.getText().length() >= 25) {
            evt.consume();
        }
        
        // TODO add your handling code here:
    }//GEN-LAST:event_entrepriseKeyTyped

    private void adresseKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_adresseKeyTyped
        if (adresse.getText().length() >= 30) {
            evt.consume();
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_adresseKeyTyped

    private void phoneKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_phoneKeyTyped
        if (phone.getText().length() >= 15) {
            evt.consume();
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_phoneKeyTyped

    private void valActionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_valActionKeyTyped

        if (valAction.getText().length() >= 12) {
            evt.consume();
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_valActionKeyTyped

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            String nomEntre =entreprise.getText().toUpperCase();
            //éliminer les espaces en début
            while(nomEntre.indexOf(" ")==0)
            {
                nomEntre=nomEntre.substring(1,nomEntre.length());
            }
            

            
            if( DbManip.existChampDsTable("entreprise", "nom_entreprise", nomEntre))
            {
             JOptionPane.showMessageDialog(null, "Ce nom existe déja !", "Attention", JOptionPane.ERROR_MESSAGE);
                entreprise.setText("");
                return;   
            }
            if (!nomEntre.replaceAll(" ", "").matches(DbManip.regexLettreNumUnderscore)){
                JOptionPane.showMessageDialog(null, "Vérifiez le nom de l'entreprise !", "Attention", JOptionPane.ERROR_MESSAGE);
                entreprise.setText("");
                return;   
            }
            
            String address = adresse.getText();
            if (!address.replaceAll(" ", "").matches(DbManip.regexLettreNumUnderscore)){
                JOptionPane.showMessageDialog(null, "L'adresse ne peut contenir que des lettres, chiffres et underscore et commence une lettre!", "Attention", JOptionPane.ERROR_MESSAGE);
                adresse.setText("");
                return;   
            }
            String tel = phone.getText().replaceAll(" ", "");
            if (!tel.matches(DbManip.regexDigit)){
                JOptionPane.showMessageDialog(null, "Le numéro ne peut contenir que des chiffres !", "Attention", JOptionPane.ERROR_MESSAGE);
                phone.setText("");
                return;   
            }
            
            String val = valAction.getText().replaceAll(" ",  "");
            if (!val.matches(DbManip.regexFloat)){
                JOptionPane.showMessageDialog(null, "La valeur ne peut contenir que des chiffres !", "Attention", JOptionPane.ERROR_MESSAGE);
                valAction.setText("");
                return;   
            }
            val = val.replace(".", ",");
            if (JOptionPane.showConfirmDialog(null, "Voulez-vous continuer ?", "Confirmation ", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) != 0) {
                return;
            }
     
            String n=String.valueOf(DbManip.next_Id("action", "id_action"));

            //ajouter une ligne à la table entreprise
            DbManip.requete = "insert into entreprise VALUES ('" +n +"','"+nomEntre+"','"+tel+"','"+address+ "')";
            DbManip.executerUpdate(DbManip.requete);
            // ajouter une ligne à la table action
            DbManip.requete = "insert into action VALUES ('" +n +"','"+n+"', TIMESTAMP '"+DbManip.date+"','"+val+ "')";
            DbManip.executerUpdate(DbManip.requete);
            //ajouter une ligne à l'historique action
            DbManip.requete = "insert into historique_action VALUES ('" +val +"', TIMESTAMP'"+DbManip.date+"','"+DbManip.next_Id("historique_action", "ID_HIS") +"','"+n+ "')";
            DbManip.executerUpdate(DbManip.requete);           
            JOptionPane.showMessageDialog(null, "L'entreprise a été ajoutée avec succès ! ", "Information", JOptionPane.INFORMATION_MESSAGE);

            // TODO add your handling code here:
        } catch (SQLException ex) {
            Logger.getLogger(AjouterEntreprise.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void effacerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_effacerActionPerformed
        entreprise.setText("");
        adresse.setText("");
        phone.setText("");
        valAction.setText("");
        
        // TODO add your handling code here:
    }//GEN-LAST:event_effacerActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField adresse;
    private javax.swing.JButton effacer;
    private javax.swing.JTextField entreprise;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField phone;
    private javax.swing.JTextField valAction;
    // End of variables declaration//GEN-END:variables
}
